# include <iostream>

#include <boost/asio.hpp>

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>

#ifndef __GNUC__
#include <crtdbg.h>
#endif

#include "StackOSI.h"
#include "LayerFactory.h"
#include "ProtocolFactory.h"
#include <memory>

using boost::asio::ip::tcp;

int main(int argc, char** argv)
{
#ifndef __GNUC__
	_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

	try
	{
		boost::asio::io_service io_service;

		tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 10000));

		for (;;)
		{
			tcp::socket socket(io_service);
			acceptor.accept(socket);


			CByteStream data;
			data.addFront("my_first_packet");

			CPacket packet(&data);

			CStackOSI::getInstance().init();

			std::cout << packet.getBufferContent() << std::endl;
			CStackOSI::getInstance().Encode(packet);
			std::cout << packet.getBufferContent() << std::endl;

			std::stringstream str;
			boost::archive::text_oarchive oar(str);

			oar << packet;

			boost::system::error_code ignored_error;
			boost::asio::write(socket, boost::asio::buffer(str.str()), ignored_error);
		}
	}
	catch (boost::archive::archive_exception& ex)
	{
		std::cerr << ex.what() << std::endl;
	}
	catch (std::exception& e)
	{
		std::cerr << e.what() << std::endl;
	}

	return 0;
}
